********************************************************************************************************
************************* Rotemberg weights (Appendix C.3) *********************************************
********************************************************************************************************

* Bring sectoral data into wide format
	clear
	use "Sector_imports.dta" 
		keep year jbsic92_formergeCH sector_real_imp_CH_gr_d4 sector_real_imp_CH_gr_d3

		rename sector_real_imp_CH_gr_d3 sector_real_imp_CH_gr_d3_
		rename sector_real_imp_CH_gr_d4 sector_real_imp_CH_gr_d4_

		drop if jbsic92_formergeCH==-88 | jbsic92_formergeCH==-99
		reshape wide sector_real_imp_CH_gr_d3 sector_real_imp_CH_gr_d4, i(year) j(jbsic92_formergeCH)

	save "Sectoral imports wide for Rotemberg.dta", replace


* Prepare BHPS data and merge sectoral import data
	clear
	use "BHPS_merged.dta"

	*** Generate variable for lagged level of nationalist attitude
		sort pid waven
		gen L3_nationalism=L3.nationalism
		
	*** Create lagged employment shares for EU support analysis (lag 3 for 2002, lag 4 for 2006)
		gen L_EU_emplsh_manufact=L3_emplsh_manufact if year==2002
			replace L_EU_emplsh_manufact=L4_emplsh_manufact if year==2006
			gen L_EU_emplsh_agri=L3_emplsh_agri if year==2002
			replace L_EU_emplsh_agri=L4_emplsh_agri if year==2006		
			gen L_EU_emplsh_fish=L3_emplsh_fish if year==2002
			replace L_EU_emplsh_fish=L4_emplsh_fish if year==2006		
			gen L_EU_emplsh_mining=L3_emplsh_mining if year==2002
			replace L_EU_emplsh_mining=L4_emplsh_mining if year==2006			
			gen L_EU_emplsh_m_food=L3_emplsh_m_food if year==2002
			replace L_EU_emplsh_m_food=L4_emplsh_m_food if year==2006				
			gen L_EU_emplsh_m_textiles=L3_emplsh_m_textiles if year==2002
			replace L_EU_emplsh_m_textiles=L4_emplsh_m_textiles if year==2006				
			gen L_EU_emplsh_m_wood=L3_emplsh_m_wood if year==2002
			replace L_EU_emplsh_m_wood=L4_emplsh_m_wood if year==2006		
			gen L_EU_emplsh_m_paper=L3_emplsh_m_paper if year==2002
			replace L_EU_emplsh_m_paper=L4_emplsh_m_paper if year==2006				
			gen L_EU_emplsh_m_coke=L3_emplsh_m_coke if year==2002
			replace L_EU_emplsh_m_coke=L4_emplsh_m_coke if year==2006				
			gen L_EU_emplsh_m_chemic=L3_emplsh_m_chemic if year==2002
			replace L_EU_emplsh_m_chemic=L4_emplsh_m_chemic if year==2006				
			gen L_EU_emplsh_m_rubber=L3_emplsh_m_rubber if year==2002
			replace L_EU_emplsh_m_rubber=L4_emplsh_m_rubber if year==2006				
			gen L_EU_emplsh_m_mineral=L3_emplsh_m_mineral if year==2002
			replace L_EU_emplsh_m_mineral=L4_emplsh_m_mineral if year==2006				
			gen L_EU_emplsh_m_basmetals=L3_emplsh_m_basmetals if year==2002
			replace L_EU_emplsh_m_basmetals=L4_emplsh_m_basmetals if year==2006		
			gen L_EU_emplsh_m_fabrmetals=L3_emplsh_m_fabrmetals if year==2002
			replace L_EU_emplsh_m_fabrmetals=L4_emplsh_m_fabrmetals if year==2006			
			gen L_EU_emplsh_m_machinery=L3_emplsh_m_machinery if year==2002
			replace L_EU_emplsh_m_machinery=L4_emplsh_m_machinery if year==2006				
			gen L_EU_emplsh_m_offmachine=L3_emplsh_m_offmachine if year==2002
			replace L_EU_emplsh_m_offmachine=L4_emplsh_m_offmachine if year==2006			
			gen L_EU_emplsh_m_elmachine=L3emplsh_m_elmachine if year==2002
			replace L_EU_emplsh_m_elmachine=L4emplsh_m_elmachine if year==2006			
			gen L_EU_emplsh_m_radio=L3_emplsh_m_radio if year==2002
			replace L_EU_emplsh_m_radio=L4_emplsh_m_radio if year==2006	
			gen L_EU_emplsh_m_instruments=L3_emplsh_m_instruments if year==2002
			replace L_EU_emplsh_m_instruments=L4_emplsh_m_instruments if year==2006			
			gen L_EU_emplsh_m_motor=L3_emplsh_m_motor if year==2002
			replace L_EU_emplsh_m_motor=L4_emplsh_m_motor if year==2006			
			gen L_EU_emplsh_m_transport=L3_emplsh_m_transport if year==2002
			replace L_EU_emplsh_m_transport=L4_emplsh_m_transport if year==2006			
			gen L_EU_emplsh_m_furniture=L3_emplsh_m_furniture if year==2002
			replace L_EU_emplsh_m_furniture=L4_emplsh_m_furniture if year==2006			
			gen L_EU_emplsh_other=L3_emplsh_other if year==2002
			replace L_EU_emplsh_other=L4_emplsh_other if year==2006		

	
	
	***** Pairwise correlations of lagged employment shares with import shock
	*** EU support
		global tradevar "real_gr_imp_CH_d_eu_nl"	
		
		* rerun main regression model with all controls				
		mixed D_EUsupport c.LDV_EUsupport##i.year ///
						$tradevar ///
						emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_eu ///
						$controls i.ID_NUTS1xYear  ///
						if (year==2006 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: || ID_lad13xyear: , stddeviations
		
		* correlation between China import shock and lagged employment shares (in observed individual-level data)
		pwcorr real_gr_imp_CH_d3_nl L_EU_emplsh_agri L_EU_emplsh_fish L_EU_emplsh_mining L_EU_emplsh_manufact L_EU_emplsh_m_food L_EU_emplsh_m_textiles L_EU_emplsh_m_wood L_EU_emplsh_m_paper L_EU_emplsh_m_coke L_EU_emplsh_m_chemic L_EU_emplsh_m_rubber L_EU_emplsh_m_mineral L_EU_emplsh_m_basmetals L_EU_emplsh_m_fabrmetals L_EU_emplsh_m_machinery L_EU_emplsh_m_offmachine L_EU_emplsh_m_elmachine L_EU_emplsh_m_radio L_EU_emplsh_m_instruments L_EU_emplsh_m_motor L_EU_emplsh_m_transport L_EU_emplsh_m_furniture L_EU_emplsh_other if e(sample)	
				
		*** Nationalism factor
		global tradevar "real_gr_imp_CH_d3_nl"
	
		* rerun main regression model with all controls				
		mixed D3_nationalism L3.nationalism ///
						$tradevar ///
						emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_nat ///
						$controls i.ID_NUTS1xYear  ///
						if (year==2008 | year==2005 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: || ID_lad13xyear: , stddeviations reml
								
		* correlation between China import shock and lagged employment shares (in observed individual-level data)
			pwcorr real_gr_imp_CH_d3_nl L3_emplsh_agri L3_emplsh_fish L3_emplsh_mining L3_emplsh_manufact L3_emplsh_m_food L3_emplsh_m_textiles L3_emplsh_m_wood L3_emplsh_m_paper L3_emplsh_m_coke L3_emplsh_m_chemic L3_emplsh_m_rubber L3_emplsh_m_mineral L3_emplsh_m_basmetals L3_emplsh_m_fabrmetals L3_emplsh_m_machinery L3_emplsh_m_offmachine L3emplsh_m_elmachine L3_emplsh_m_radio L3_emplsh_m_instruments L3_emplsh_m_motor L3_emplsh_m_transport L3_emplsh_m_furniture L3_emplsh_other if e(sample)
	

			
			
	*** Create numerical version of lagged employment shares	
		clonevar L3_emplsh_1=L3_emplsh_agri 
		clonevar L3_emplsh_2=L3_emplsh_fish 
		clonevar L3_emplsh_3=L3_emplsh_mining
		clonevar L3_emplsh_4=L3_emplsh_m_food
		clonevar L3_emplsh_5=L3_emplsh_m_textile
		clonevar L3_emplsh_6=L3_emplsh_m_wood
		clonevar L3_emplsh_7=L3_emplsh_m_paper
		clonevar L3_emplsh_8=L3_emplsh_m_coke
		clonevar L3_emplsh_9=L3_emplsh_m_chemic
		clonevar L3_emplsh_10=L3_emplsh_m_rubber
		clonevar L3_emplsh_11=L3_emplsh_m_mineral
		clonevar L3_emplsh_12=L3_emplsh_m_basmetals
		clonevar L3_emplsh_13=L3_emplsh_m_fabrmetal
		clonevar L3_emplsh_14=L3_emplsh_m_machinery
		clonevar L3_emplsh_15=L3_emplsh_m_offmachine
		clonevar L3_emplsh_16=L3emplsh_m_elmachine
		clonevar L3_emplsh_17=L3_emplsh_m_radio
		clonevar L3_emplsh_18=L3_emplsh_m_instruments
		clonevar L3_emplsh_19=L3_emplsh_m_motor
		clonevar L3_emplsh_20=L3_emplsh_m_transport
		clonevar L3_emplsh_21=L3_emplsh_m_furniture

		clonevar L4_emplsh_1=L4_emplsh_agri 
		clonevar L4_emplsh_2=L4_emplsh_fish 
		clonevar L4_emplsh_3=L4_emplsh_mining
		clonevar L4_emplsh_4=L4_emplsh_m_food
		clonevar L4_emplsh_5=L4_emplsh_m_textile
		clonevar L4_emplsh_6=L4_emplsh_m_wood
		clonevar L4_emplsh_7=L4_emplsh_m_paper
		clonevar L4_emplsh_8=L4_emplsh_m_coke
		clonevar L4_emplsh_9=L4_emplsh_m_chemic
		clonevar L4_emplsh_10=L4_emplsh_m_rubber
		clonevar L4_emplsh_11=L4_emplsh_m_mineral
		clonevar L4_emplsh_12=L4_emplsh_m_basmetals
		clonevar L4_emplsh_13=L4_emplsh_m_fabrmetal
		clonevar L4_emplsh_14=L4_emplsh_m_machinery
		clonevar L4_emplsh_15=L4_emplsh_m_offmachine
		clonevar L4_emplsh_16=L4emplsh_m_elmachine
		clonevar L4_emplsh_17=L4_emplsh_m_radio
		clonevar L4_emplsh_18=L4_emplsh_m_instruments
		clonevar L4_emplsh_19=L4_emplsh_m_motor
		clonevar L4_emplsh_20=L4_emplsh_m_transport
		clonevar L4_emplsh_21=L4_emplsh_m_furniture
		
		clonevar L_EU_emplsh_1=L_EU_emplsh_agri 
		clonevar L_EU_emplsh_2=L_EU_emplsh_fish 
		clonevar L_EU_emplsh_3=L_EU_emplsh_mining
		clonevar L_EU_emplsh_4=L_EU_emplsh_m_food
		clonevar L_EU_emplsh_5=L_EU_emplsh_m_textile
		clonevar L_EU_emplsh_6=L_EU_emplsh_m_wood
		clonevar L_EU_emplsh_7=L_EU_emplsh_m_paper
		clonevar L_EU_emplsh_8=L_EU_emplsh_m_coke
		clonevar L_EU_emplsh_9=L_EU_emplsh_m_chemic
		clonevar L_EU_emplsh_10=L_EU_emplsh_m_rubber
		clonevar L_EU_emplsh_11=L_EU_emplsh_m_mineral
		clonevar L_EU_emplsh_12=L_EU_emplsh_m_basmetals
		clonevar L_EU_emplsh_13=L_EU_emplsh_m_fabrmetal
		clonevar L_EU_emplsh_14=L_EU_emplsh_m_machinery
		clonevar L_EU_emplsh_15=L_EU_emplsh_m_offmachine
		clonevar L_EU_emplsh_16=L_EU_emplsh_m_elmachine
		clonevar L_EU_emplsh_17=L_EU_emplsh_m_radio
		clonevar L_EU_emplsh_18=L_EU_emplsh_m_instruments
		clonevar L_EU_emplsh_19=L_EU_emplsh_m_motor
		clonevar L_EU_emplsh_20=L_EU_emplsh_m_transport
		clonevar L_EU_emplsh_21=L_EU_emplsh_m_furniture
		
	*** drop alphabetical version of the lagged employment share variables
		drop L3_emplsh_manufact - L3_emplsh_other
		drop L4_emplsh_manufact - L4_emplsh_other
		drop L_EU_emplsh_manufact- L_EU_emplsh_other
		
			
	*** Merge sectoral import growth data	
		* merge import growth rate by sector to data (merge on year)		
		merge m:1 year using "Sectoral imports wide for Rotemberg.dta"

		* shorten variable name
		forvalues i = 1 (1) 21 {
		rename sector_real_imp_CH_gr_d3_`i' sect_real_imp_CH_gr_d3_`i'
		rename sector_real_imp_CH_gr_d4_`i' sect_real_imp_CH_gr_d4_`i'
		}	
		
		* create sectoral import growth rates for EU support analysis (lag 3 for 2002, lag 4 for 2006)
		forvalues i = 1 (1) 21 {
		gen sec_real_imp_CH_gr_d_eu_`i'=sect_real_imp_CH_gr_d3_`i' if year==2002
		replace sec_real_imp_CH_gr_d_eu_`i'=sect_real_imp_CH_gr_d4_`i' if year==2006
		}		
		

************* Collapse data to NUTS3-year-structure ****************************	
	collapse (mean) D3_nationalism D_EUsupport LDV_EUsupport L3_nationalism ///
					real_gr_imp_CH_d3_nl real_gr_imp_CH_d_eu_nl ///
					L3_emplsh_*  L4_emplsh_* L_EU_emplsh_* ///
					sect_real_imp_CH_gr_d3_* sect_real_imp_CH_gr_d4_* sec_real_imp_CH_gr_d_eu_* ///
					emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_nat pop_sharenonUK_d_eu ///
					age ///
					ID_NUTS1xYear  year, by(ID_NUTS3xYear)
						 
*** create year dummies
	gen year2005=0 if year<.
	replace year2005=1 if year==2005
	gen year2006=0 if year<.
	replace year2006=1 if year==2006						
	gen year2008=0 if year<.
	replace year2008=1 if year==2008


*** Check for missing values in employment shares and sectoral growth rates, and enter zero 
	// otherwise this produced an error in the bartikweight command	
	sum L3_emplsh_1- L3_emplsh_21
	replace L3_emplsh_1=0 if L3_emplsh_1==.	& L3_emplsh_2<.
	sum L4_emplsh_1-L4_emplsh_21
	replace L4_emplsh_1=0 if L4_emplsh_1==.	& L4_emplsh_2<.
	sum L_EU_emplsh_1- L_EU_emplsh_21
	replace L_EU_emplsh_1=0 if L_EU_emplsh_1==.	& L_EU_emplsh_2<.
	
	
******************** Regressions on collapsed data *****************************
	* EU membership support
	reg D_EUsupport real_gr_imp_CH_d_eu_nl LDV_EUsupport
		eststo EU1
	reg D_EUsupport real_gr_imp_CH_d_eu_nl c.LDV_EUsupport##i.i.year
		eststo EU2
	reg D_EUsupport real_gr_imp_CH_d_eu_nl c.LDV_EUsupport##i.i.year emplsh_manufact_1998 i.year
		eststo EU3
	reg D_EUsupport real_gr_imp_CH_d_eu_nl c.LDV_EUsupport##i.i.year emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_eu 
		eststo EU4

	esttab EU1 EU2 EU3 EU4 using "Regressions on NUTS3 level data_EUsupport.rtf", ///
		b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars(r2) varwidth(30) nogaps compress ///
		label replace	
		
	* Nationalist attitude
	reg D3_nationalism real_gr_imp_CH_d3_nl L3_nationalism
		eststo NAT1
	reg D3_nationalism real_gr_imp_CH_d3_nl L3_nationalism i.year
		eststo NAT2
	reg D3_nationalism real_gr_imp_CH_d3_nl L3_nationalism emplsh_manufact_1998 i.year
		eststo NAT3
	reg D3_nationalism real_gr_imp_CH_d3_nl L3_nationalism emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_nat i.year
		eststo NAT4
		
	esttab NAT1 NAT2 NAT3 NAT4 using "Regressions on NUTS3 level data_Nationalism.rtf", ///
		b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars(r2) varwidth(30) nogaps compress ///
		label replace
	 

	
********************** Rotemberg weights**************************
		// computed for single years
		// means of yearly values calculated in Excel tables based on matrices from below 		
	
	*** EU membership support
		* 2002
		preserve 
		drop if year!=2002
		drop if LDV_EUsupport==. | D_EUsupport==.
		
		local controls LDV_EUsupport 
		local y D_EUsupport 
		local x real_gr_imp_CH_d_eu_nl
		local ind_stub L_EU_emplsh_
		local growth_stub sec_real_imp_CH_gr_d_eu_

		bartik_weight, z(`ind_stub'*) weightstub(`growth_stub'*) x(`x') y(`y') controls(`controls')
		
		mat alpha = r(alpha)
		matrix list alpha

		restore

		* 2006
		preserve 
		drop if year!=2006
		drop if LDV_EUsupport==. | D_EUsupport==.

		local controls LDV_EUsupport 
		local y D_EUsupport 
		local x real_gr_imp_CH_d_eu_nl
		local ind_stub L_EU_emplsh_
		local growth_stub sec_real_imp_CH_gr_d_eu_

		bartik_weight, z(`ind_stub'*) weightstub(`growth_stub'*) x(`x') y(`y') controls(`controls')

		mat alpha = r(alpha)
		matrix list alpha	
			
		restore
		

	*** Nationalist attitudes
		* 2008
		preserve 
		drop if year!=2008
		
		local controls L3_nationalism
		local y D3_nationalism 
		local x real_gr_imp_CH_d3_nl
		local ind_stub L3_emplsh_
		local growth_stub sect_real_imp_CH_gr_d3_

		bartik_weight, z(`ind_stub'*) weightstub(`growth_stub'*) x(`x') y(`y') controls(`controls') 
				
		mat alpha = r(alpha)
		matrix list alpha

		restore

		* 2005
		preserve 
		drop if year!=2005
		
		local controls L3_nationalism
		local y D3_nationalism 
		local x real_gr_imp_CH_d3_nl
		local ind_stub L3_emplsh_
		local growth_stub sect_real_imp_CH_gr_d3_

		bartik_weight, z(`ind_stub'*) weightstub(`growth_stub'*) x(`x') y(`y') controls(`controls')
				
		mat alpha = r(alpha)
		matrix list alpha

		restore
		
		* 2002
		preserve 
		drop if year!=2002
		* drop L3_emplsh_1 sect_real_imp_CH_gr_d3_1
		
		local controls L3_nationalism
		local y D3_nationalism 
		local x real_gr_imp_CH_d3_nl
		local ind_stub L3_emplsh_
		local growth_stub sect_real_imp_CH_gr_d3_

		bartik_weight, z(`ind_stub'*) weightstub(`growth_stub'*) x(`x') y(`y') controls(`controls')
			
		mat alpha = r(alpha)
		matrix list alpha

		restore